/**
 * Description:
 * Class Name:CharsetTransform
 * Date:2022/5/3 19:11
 *
 * @author LTisme
 * @version JAVA17
 */
package 疯狂Java讲义.第15章_输入输出._15_9_NIO._15_9_4_字符集和Charset;

import java.nio.ByteBuffer;
import java.nio.CharBuffer;
import java.nio.charset.Charset;
import java.nio.charset.CharsetDecoder;
import java.nio.charset.CharsetEncoder;

public class CharsetTransform {
    public static void main(String[] args) throws Exception{
        Charset charset = Charset.forName("GBK");
        CharsetDecoder charsetDecoder = charset.newDecoder();
        CharsetEncoder charsetEncoder = charset.newEncoder();

        // 创建一个CharBuffer对象
        CharBuffer cbuff = CharBuffer.allocate(8);
        cbuff.put('孙');
        cbuff.put('悟');
        cbuff.put('空');
        // 使用GBK编码器来编码字符
        cbuff.flip();
        ByteBuffer bbuff = charsetEncoder.encode(cbuff);
        for (int i = 0; i < bbuff.capacity(); i++) {
            System.out.println(bbuff.get(i) + "");
        }
        // 将ByteBuffer的数据解码成字符序列
        System.out.println("\n" + charsetDecoder.decode(bbuff));

    }
}
